<template>
	<view class="Register_content Memory_key">
		<view class="register_box major_box">
			<view class="register_title">
				<image src="../../static/image/major_back_icon.png" @tap="back"></image>
				<text  style="color:#C2C9DF;" :style="i18n.yzzlcregister_titlestyle">{{i18n.yzzlcregister_title}}</text>
			</view>
			
			<view class="tips_box">
				<text class="tips_title">{{i18n.yzzltips_title}}</text>
				<view class="text_item">
					<text>{{i18n.ps2}}</text>
				</view>
				<view class="keyworld verification_key">
					<!-- <input v-for="(item,current) in random" type="text" v-model="first_key" disabled  @tap="selectKey"/> -->
					<input type="text" v-model="first_key" disabled  @tap="selectKey(1)"/>
					<input type="text" v-model="two_key" disabled  @tap="selectKey(2)"/>
					<input type="text" v-model="three_key" disabled  @tap="selectKey(3)"/>
				</view>
				<view class="keyworld verification_key">
					<text  v-for="(item,k) in random" :key="k">第{{item}}位</text>
				</view>
			</view>
			<view class="tips_box">
				<view class="keyworld">
					<text v-for="(item,index) in mnemonic" :key="index" @tap="handleKye(item)">
						{{item}}
					</text>
				</view>
			</view>
			
			<image :src="i18n.netbtnimg" class="backups next_btn"style="margin-top: 0;" @tap="next"></image>
		
		</view>
	</view>
</template>

<script>
	export default {
		computed: {
			i18n() {
				return this.$t('user')
			}
		},
		data() {
			
			return {
				mnemonic:"",
				random:"",
				first_key:"",
				two_key:"",
				three_key:"",
				Wallet:"",
				validateKey:[],
			}
		},
		onLoad(options) {
			console.log(options)
			const testArray = JSON.parse(options.mnemonic)
			if (!Array.prototype.derangedArray) {
			    Array.prototype.derangedArray = function() {
			        for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
			        return this;
			    };
			}
			this.mnemonic = testArray.derangedArray();
			const length = this.mnemonic.length;
			this.random = this.$CommonJS.random(3,length,1);
			this.random.map((item,index)=>{
				const n = item - 1;
				this.validateKey.push(JSON.parse(options.mnemonic)[n])
			})
			console.log(this.validateKey)
		},
		methods: {
			back(){
				this.$CommonJS.navigateBack()
			},
			next(){
				const _this=this
				if(
				this.first_key == this.validateKey[0] &&
				this.two_key == this.validateKey[1] &&
				this.three_key == this.validateKey[2] 
				){
					this.$CommonJS.navigateTo('./major_user')
				}else{
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '助记词验证失败':'Mnemonic verification failed')
				}
				
			},
			selectKey(n){
				//console.log(n)
				switch(n){
					case 1:
						//第一位
						if(this.first_key != "" ){
							this.first_key = ""
						}
						break;
					case 2:
						//第一位
						if(this.two_key != "" ){
							this.two_key = ""
						}
						break;
					case 3:
						//第一位
						if(this.three_key != "" ){
							this.three_key = ""
						}
						break;
				}
				
			},
			handleKye(item){
				if(this.first_key == ""){
					this.first_key = item;
					return;
				}else if(this.two_key == ""){
					this.two_key = item;
					return;
				}else if(this.three_key == ""){
					this.three_key = item;
					return;
				}else{
					return;
				}
			}
		}
	}
</script>

<style lang="stylus">
	@import "../../static/stylus/register/memory.stylus"
</style>
